package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConst;
import com.itheima.constant.RedisConst;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.service.MemberService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import java.util.Date;
import java.util.Map;

@RestController
@RequestMapping("/login")
@Slf4j
public class LoginController {

	@Reference
	MemberService memberService;

	@Autowired
	JedisPool jedisPool;

	@RequestMapping("/check")
	public Result check(@RequestBody Map<String, String> map) {

		String telephone = map.get("telephone");
		String validateCode = map.get("validateCode");

		//从redis中获取验证码
		String validateCodeInRedis = jedisPool.getResource().get(RedisConst.SENDTYPE_LOGIN +"-"+ telephone);

		if(validateCodeInRedis == null){
			return new Result(false,"验证码失效");
		}

		if(!validateCodeInRedis.equals(validateCode)){

			return new Result(false, MessageConst.LOGIN_FAULSE);

		}


		//保存会员信息
		Member member = new Member();
		member.setPhoneNumber(telephone);
		member.setRegTime(new Date());
		memberService.save(member);


		return new Result(false, MessageConst.LOGIN_SUCCESS);

	}

}
